<template>
  <div class="box">
    <div class="app">
      <div class="iag">
        <div class="img">
          <img src="../assets/logo.png" alt="" />
        </div>
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="ruleForm.username"
            placeholder="输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            placeholder="输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/login.js'
export default {
  data() {
    return {
      ruleForm: {
        username: 'admin',
        password: '123456',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
      },
    }
  },
  mounted() {},
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login(this.ruleForm).then((res) => {
            console.log('登录', res)
            if (res.data.meta.status == 200) {
              localStorage.setItem('token', res.data.data.token)
              this.$message.success(res.data.meta.msg)
              this.$router.push('/about')
            } else {
              this.$message.error(res.data.meta.msg)
            }
          })
        } else {
          // console.log('输入的用户名密码错误')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
  },
  computed: {},
}
</script>

<style scoped lang="scss">
.box {
  width: 100%;
  height: 100vh;
  background-color: #1d5e6b;
  position: relative;
}
.app {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 500px;
  height: 300px;
  // background: rgb(238, 236, 236);
  background: rgba(238, 236, 236, 0.2);
  box-shadow: 0 0 0 20px rgba(132, 203, 231, 0.5);
  position: relative;
  padding-top: 70px;
  padding-right: 20px;
  .iag {
    width: 120px;
    height: 120px;
    background: #ffffff;
    border-radius: 50%;
    position: absolute;
    top: -26%;
    left: 38%;
    box-shadow: 0px 0px 20px 0px;
    color: white;
    .img {
      width: 100px;
      height: 100px;
      background: #eeeeee;
      border-radius: 50%;
      overflow: hidden;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        margin-top: 6px;
      }
    }
  }
}
</style>
